package com.yanqu.road.server.manger.activity.chefduel;


import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;

import com.yanqu.road.entity.autodata.chefduel.config.*;
import com.yanqu.road.entity.enums.activity.chefDuel.eChefDuelActivityType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.chefduel.ChefDuel1Config;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChefDuelPhase1Proto;

import com.yanqu.road.pb.cookboyactivity.CookBoyActivityProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.chefduel.ChefDuelPhase1Module;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;


import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 厨神争霸一阶段
 */

public class ChefDuelPhase1Mgr extends TempMgr {

    private static ChefDuel1Config config;

    private static Map<String, ChefDuelPhase1Proto.ChefDuelPhase1ActivityMsgSync.Builder> configMsgMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    /**
     * 厨神对决一阶段刷新配置
     */
    public static void reloadChefDuelPhase1Config() {
        getLogger().info("reload ChefDuelPhase1Config start");
        List<ActivityInfo> openActivityInfo = NormalActivityMgr.getOpenActivityInfoList(eActivityType.ChefDuel.getValue(), eChefDuelActivityType.Phase1.getChildType());
        if (openActivityInfo == null || openActivityInfo.isEmpty()) {
            config = null;
            configMsgMap.clear();
            getLogger().info("reload ChefDuelPhase1Config end, no activity info open");
            return;
        }
        ActivityInfo activityInfo = openActivityInfo.get(0);
        Map<String, ActivityConfig> activityConfigMap = ActivityMgr.getActivityConfigMap(activityInfo.getActivityId());

        ChefDuel1Config tmpConfig = new ChefDuel1Config(activityInfo,activityConfigMap);
        config = tmpConfig;
        configMsgMap.clear();
        //在线玩家检测
        for (GamePlayer gamePlayer:GamePlayerMgr.getAllOnlinePlayer()){
            gamePlayer.getModule(ChefDuelPhase1Module.class).checkAndSendData();
        }
        //跨服更新一下
        crossRefreshPhase1Activity();
        getLogger().info("reload ChefDuelPhase1Config end ,activityId {}", activityInfo.getActivityId());
    }



    /**
     * 取得阶段1的配置msg
     */
    public static ChefDuelPhase1Proto.ChefDuelPhase1ActivityMsgSync.Builder getParse1ConfigMsg(String language){

        if (!configMsgMap.containsKey(language)){
            synchronized (configMsgMap){
                if (!configMsgMap.containsKey(language)){
                    ChefDuelPhase1Proto.ChefDuelPhase1ActivityMsgSync.Builder builder = buildChefDuelPhase1ConfigMsg( language);
                    configMsgMap.put(language,builder);
                }
            }
        }
        return configMsgMap.get(language);
    }

    public static ChefDuel1Config getConfig(){
        return config;
    }

    /**
     *  阶段1的配置转msg
     */
    public static ChefDuelPhase1Proto.ChefDuelPhase1ActivityMsgSync.Builder buildChefDuelPhase1ConfigMsg(String language){
        if (config == null){
            return null;
        }
        ActivityInfo activityInfo = config.getActivityInfo();
        if (activityInfo == null){
            return null;
        }
        ChefDuelPhase1Proto.ChefDuelPhase1ActivityMsgSync.Builder builder = ChefDuelPhase1Proto.ChefDuelPhase1ActivityMsgSync.newBuilder();

        //config
        if (config!=null){
            //关卡配置
            Map<Integer, ChefDuel1MissionConfig> missionConfigMap = config.getMissionConfigMap();
            for (ChefDuel1MissionConfig missionConfig : missionConfigMap.values()){
                ChefDuelPhase1Proto.ChefDuelPhase1MissionConfigMsg.Builder missBuilder = ChefDuelPhase1Proto.ChefDuelPhase1MissionConfigMsg.newBuilder();
                missBuilder.setId(missionConfig.getId());
                missBuilder.setName(ServerLanguageMgr.getContent(missionConfig.getName(),language));
                missBuilder.setType(missionConfig.getType());
                missBuilder.setScoreLimit(missionConfig.getScoreLimit());
                missBuilder.setScorePass(missionConfig.getScorePass());
                missBuilder.setDeviceParam(missionConfig.getDeviceParam());
                missBuilder.setGuestDemand(missionConfig.getGuestDemand());
                missBuilder.setGuestWaitTime(missionConfig.getGuestWaitTime());
                missBuilder.setGuestSupplementTime(missionConfig.getGuestSupplementTime());
                missBuilder.setGuestType(missionConfig.getGuestType());
                missBuilder.setEvaluate(missionConfig.getEvaluate());
                missBuilder.setReward(missionConfig.getReward());
                missBuilder.setDishType(missionConfig.getDishType());
                missBuilder.setTime(missionConfig.getTime());
                missBuilder.setInitialGuestParam(missionConfig.getInitialGuestParam());
                missBuilder.setPunishTime(missionConfig.getPunishTime());
                missBuilder.setBackground(missionConfig.getBackground());
                builder.addMissionConfig(missBuilder);
            }
            //技能配置
            Map<Integer, ChefDuel1TalentConfig> talentConfigMap = config.getTalentConfigMap();
            for (ChefDuel1TalentConfig talentConfig : talentConfigMap.values()){
                ChefDuelPhase1Proto.ChefDuelPhase1TalentConfigMsg.Builder talentBuilder = ChefDuelPhase1Proto.ChefDuelPhase1TalentConfigMsg.newBuilder();
                talentBuilder.setId(talentConfig.getId());
                talentBuilder.setType(talentConfig.getType());
                talentBuilder.setParam(talentConfig.getParam());
                talentBuilder.setName(ServerLanguageMgr.getContent(talentConfig.getName(),language));
                talentBuilder.setDesc(ServerLanguageMgr.getContent(talentConfig.getDesc(),language));
                talentBuilder.setUnlockTalents(talentConfig.getUnlockTalents());
                talentBuilder.setMaxLevel(talentConfig.getMaxLevel());
                talentBuilder.setConsume(talentConfig.getConsume());
                talentBuilder.setPopup(talentConfig.getPopup());
                talentBuilder.setPopupDesc(ServerLanguageMgr.getContent(talentConfig.getPopupDesc(),language));
                builder.addTalentConfig(talentBuilder);
            }
            //设备配置
            Map<Integer, ChefDuel1DeviceConfig> deviceConfigMap = config.getDeviceConfigMap();
            for (ChefDuel1DeviceConfig deviceConfig : deviceConfigMap.values()){
                ChefDuelPhase1Proto.ChefDuelPhase1DeviceConfigMsg.Builder deviceB = ChefDuelPhase1Proto.ChefDuelPhase1DeviceConfigMsg.newBuilder();
                deviceB.setId(deviceConfig.getId());
                deviceB.setType(deviceConfig.getType());
                deviceB.setLevelUpEffect(deviceConfig.getLevelUpEffect());
                deviceB.setName(ServerLanguageMgr.getContent(deviceConfig.getName(),language));
                deviceB.setDesc(ServerLanguageMgr.getContent(deviceConfig.getDesc(),language));
                deviceB.setAppearance(deviceConfig.getAppearance());
                deviceB.setAutoParam(deviceConfig.getAutoParam());
                builder.addDeviceConfig(deviceB);
            }
            //操作对象配置
            Map<Integer, ChefDuel1ObjectConfig> objectConfigMap = config.getObjectConfigMap();
            for (ChefDuel1ObjectConfig objectConfig : objectConfigMap.values()){
                ChefDuelPhase1Proto.ChefDuelPhase1ObjectConfigMsg.Builder objBuild = ChefDuelPhase1Proto.ChefDuelPhase1ObjectConfigMsg.newBuilder();
                objBuild.setId(objectConfig.getId());
                objBuild.setType(objectConfig.getType());
                objBuild.setSourceObject(objectConfig.getSourceObject());
                objBuild.setTriggerMode(objectConfig.getTriggerMode());
                objBuild.setClassifyObject(objectConfig.getClassifyObject());
                objBuild.setName(ServerLanguageMgr.getContent(objectConfig.getName(),language));
                objBuild.setDesc(ServerLanguageMgr.getContent(objectConfig.getDesc(),language));
                objBuild.setSize(objectConfig.getSize());
                objBuild.setMaxNum(objectConfig.getMaxNum());
                builder.addObjConfig(objBuild);
            }

            //菜品配置
            Map<Integer, ChefDuel1DishConfig> dishConfigMap = config.getDishConfigMap();
            for (ChefDuel1DishConfig dishConfig : dishConfigMap.values()){
                ChefDuelPhase1Proto.ChefDuelPhase1DishConfigMsg.Builder dishB = ChefDuelPhase1Proto.ChefDuelPhase1DishConfigMsg.newBuilder();
                dishB.setId(dishConfig.getId());
                dishB.setName(ServerLanguageMgr.getContent(dishConfig.getName(),language));
                dishB.setDesc(ServerLanguageMgr.getContent(dishConfig.getDesc(),language));
                dishB.setPrice(dishConfig.getPrice());
                dishB.setSize(dishConfig.getSize());
                dishB.setMaxNum(dishConfig.getMaxNum());
                builder.addDishConfig(dishB);
            }

            //等级配置
            Map<Integer, ChefDuel1LevelConfig> levelConfigMap = config.getLevelConfigMap();
            for (ChefDuel1LevelConfig levelConfig : levelConfigMap.values()){
                ChefDuelPhase1Proto.ChefDuelPhase1LevelConfigMsg.Builder levelB = ChefDuelPhase1Proto.ChefDuelPhase1LevelConfigMsg.newBuilder();
                levelB.setId(levelConfig.getId());
                levelB.setExp(levelConfig.getExp());
                levelB.setGetTalentPoints(levelConfig.getGetTalentPoints());
                builder.addLevelConfig(levelB);
            }

            //
            Map<Integer, ChefDuel1GuestConfig> guestConfigMap = config.getGuestConfigMap();
            for (ChefDuel1GuestConfig guestConfig : guestConfigMap.values()){
                ChefDuelPhase1Proto.ChefDuelPhase1GuestConfigMsg.Builder guestB = ChefDuelPhase1Proto.ChefDuelPhase1GuestConfigMsg.newBuilder();
                guestB.setId(guestConfig.getId());
                guestB.setType(guestConfig.getType());
                guestB.setEntrySpeed(guestConfig.getEntrySpeed());
                guestB.setDepartureSpeed(guestConfig.getDepartureSpeed());
                guestB.setClickNum(guestConfig.getClickNum());
                guestB.setName(ServerLanguageMgr.getContent(guestConfig.getName(),language));
                guestB.setDesc(ServerLanguageMgr.getContent(guestConfig.getDesc(),language));
                guestB.setWeight(guestConfig.getWeight());
                builder.addGuestConfig(guestB);
            }
        }
        return builder;
    }

    /**
     * 扔到跨服让relationMgr刷新一下
     */
    public static void crossRefreshPhase1Activity(){
        if (config == null){
            return;
        }
        ActivityInfo activityInfo = config.getActivityInfo();
        //不在展示期间
        if (!ActivityMgr.activityInShowTime(activityInfo)){
            return;
        }
        //拿服务器列表
        List<Long> serverIdList = activityInfo.getServerIdList();
        if (serverIdList == null || serverIdList.isEmpty()){
            return;
        }
        //只有列表中的第一个服参与更新
        long serverId = serverIdList.get(0);
        if(GameServer.getInstance().getServerId() != serverId){
            return;
        }
        long crossId = activityInfo.getCrossId();
        if (crossId != 0){
            CookBoyActivityProto.CrossRelationActivityReloadMsg.Builder msg = CookBoyActivityProto.CrossRelationActivityReloadMsg.newBuilder();
            msg.setActivityId(activityInfo.getActivityId());
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_CHEF_DUEL_PHASE_1_RELOAD, msg);
            GamePlayerMgr.sendPacket(0, pbMsg);
        }
    }

}
